package com.dimples.dd.system.controller;

import com.dimples.dd.common.constant.DDServerConstant;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.common.result.CommonResult;
import com.dimples.dd.log.core.annotation.ApiLog;
import com.dimples.dd.log.core.enums.OperateTypeEnum;
import com.dimples.dd.system.model.form.NoticeForm;
import com.dimples.dd.system.model.query.NoticePageQuery;
import com.dimples.dd.system.model.vo.NoticeDetailVO;
import com.dimples.dd.system.model.vo.NoticePageVO;
import com.dimples.dd.system.model.vo.UserNoticePageVO;
import com.dimples.dd.system.service.SystemNoticeService;
import com.dimples.dd.system.service.SystemUserNoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.simpleframework.xml.core.Validate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 通知公告前端控制层
 *
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/12/30
 */
@Tag(name = "通知公告接口")
@Validate
@RestController
@RequestMapping("notices")
@RequiredArgsConstructor
public class SysNoticeController {

    private final SystemNoticeService noticeService;

    private final SystemUserNoticeService userNoticeService;

    @Operation(summary = "通知公告分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:notice:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public PageResult<NoticePageVO> selectNoticePage(NoticePageQuery queryParams) {
        return this.noticeService.selectNoticePage(queryParams);
    }

    @Operation(summary = "新增通知公告")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:notice:create')")
    @ApiLog(operateType = OperateTypeEnum.CREATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> createNotice(@RequestBody @Valid NoticeForm formData) {
        return CommonResult.success(this.noticeService.createNotice(formData));
    }

    @Operation(summary = "获取通知公告表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:notice:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<NoticeForm> selectNoticeForm(
            @Parameter(description = "通知公告ID") @PathVariable Long id
    ) {
        return CommonResult.success(this.noticeService.selectNoticeFormData(id));
    }

    @Operation(summary = "阅读获取通知公告详情")
    @GetMapping("/{id}/detail")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<NoticeDetailVO> selectNoticeDetail(
            @Parameter(description = "通知公告ID") @PathVariable Long id
    ) {
        NoticeDetailVO detailVO = this.noticeService.selectNoticeDetail(id);
        return CommonResult.success(detailVO);
    }

    @Operation(summary = "修改通知公告")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:notice:update')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> updateNotice(
            @Parameter(description = "通知公告ID") @PathVariable Long id,
            @RequestBody @Valid NoticeForm formData
    ) {
        return CommonResult.success(this.noticeService.updateNotice(id, formData));
    }

    @Operation(summary = "删除通知公告")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:notice:delete')")
    @ApiLog(operateType = OperateTypeEnum.DELETE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> deleteNotices(
            @Parameter(description = "通知公告ID，多个以英文逗号(,)分割") @PathVariable @NotBlank(message = "不能为空") String ids
    ) {
        return CommonResult.success(this.noticeService.deleteNotices(ids));
    }

    @Operation(summary = "发布通知公告")
    @PatchMapping(value = "/{id}/publish")
    @PreAuthorize("@ss.hasPerm('system:notice:publish')")
    @ApiLog(operateType = OperateTypeEnum.PUBLISH, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> publishNotice(
            @Parameter(description = "通知公告ID") @PathVariable Long id
    ) {
        return CommonResult.success(this.noticeService.publishNotice(id));
    }

    @Operation(summary = "撤回通知公告")
    @PatchMapping(value = "/{id}/revoke")
    @PreAuthorize("@ss.hasPerm('system:notice:revoke')")
    @ApiLog(operateType = OperateTypeEnum.REVOKE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> revokeNotice(
            @Parameter(description = "通知公告ID") @PathVariable Long id
    ) {
        return CommonResult.success(this.noticeService.revokeNotice(id));
    }

    @Operation(summary = "全部已读")
    @PutMapping("/read-all")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> readAll() {
        return CommonResult.success(this.userNoticeService.readAll());
    }

    @Operation(summary = "获取我的通知公告分页列表")
    @GetMapping("/my-page")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public PageResult<UserNoticePageVO> selectMyNoticePage(NoticePageQuery queryParams) {
        return this.noticeService.selectMyNoticePage(queryParams);
    }
}
